﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BaseFunction
{
    public class RequestValidate
    {

        /// <summary>
        /// 获取Form请求中 指定key的值
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public static int GetFormInt(string Key, int DefaultValue)
        {
            return StringHelp.IsNumberAndNotnull(System.Web.HttpContext.Current.Request.Form[Key], DefaultValue);
        }


        /// <summary>
        /// 获取Form请求中 指定key的值
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public static string GetFormString(string Key, string DefaultValue)
        {
            string Value = StringHelp.GetSafeStr(System.Web.HttpContext.Current.Request.Form[Key] + "");
            return Value == "" ? DefaultValue : Value;
        }

        /// <summary>
        /// 获取Form请求中 指定key的值
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public static Guid GetFormGuid(string Key)
        {
            Guid result;
            if (Guid.TryParse(System.Web.HttpContext.Current.Request.Form[Key], out result))
            {
                return result;
            }
            return new Guid();                   
        }

        /// <summary>
        /// 获取Get请求中 指定key的值
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public static string GetQueryString(string Key, string DefaultValue)
        {
            string Value = StringHelp.GetSafeStr(System.Web.HttpContext.Current.Request.QueryString[Key] + "");
            return Value == "" ? DefaultValue : Value;
        }

        /// <summary>
        /// 获取Get请求中 指定key的值
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public static int GetQueryInt(string Key, int DefaultValue)
        {
            return StringHelp.IsNumberAndNotnull(System.Web.HttpContext.Current.Request.QueryString[Key], DefaultValue);
        }


        /// <summary>
        /// 获取Get请求中 指定key的Guid
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public static Guid GetQueryIGuid(string Key)
        {
            Guid result;
            if (Guid.TryParse(System.Web.HttpContext.Current.Request.QueryString[Key], out result))
            {
                return result;
            }
            return new Guid();    
        }
        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            if (string.IsNullOrEmpty(result))
                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
                result = HttpContext.Current.Request.UserHostAddress;
            if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
                return "127.0.0.1";
            return result;
        }
    }
}
